package com.gzhu.knowledgeAdmin.service.impl;

import com.gzhu.knowledgeAdmin.common.dto.intention.IntentionAddRequest;
import com.gzhu.knowledgeAdmin.common.dto.intention.IntentionDeleteRequest;
import com.gzhu.knowledgeAdmin.common.dto.intention.IntentionGetRequest;
import com.gzhu.knowledgeAdmin.common.dto.intention.IntentionUpdateRequest;
import com.gzhu.knowledgeAdmin.common.vo.IntentionVo;
import com.gzhu.knowledgeAdmin.entity.Intention;
import com.gzhu.knowledgeAdmin.repository.IntentionRepository;
import com.gzhu.knowledgeAdmin.service.IntentionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IntentionServiceImpl implements IntentionService {

    @Autowired
    private IntentionRepository intentionRepository;


    /**
     * 新增意向
     * @param intentionAddRequest
     */
    @Override
    public void addIntention(IntentionAddRequest intentionAddRequest) {
        Intention intention = new Intention();
        BeanUtils.copyProperties(intentionAddRequest, intention);
        // 设置判断条件，改为字符串，“，”分隔
        String allCondition = StringUtils.join(intentionAddRequest.getAllCondition(), ",");
        intention.setAllCondition(allCondition);;
        String anyCondition = StringUtils.join(intentionAddRequest.getAnyCondition(), ",");
        intention.setAnyCondition(anyCondition);

        // TODO intentionType为0的时候，需要根据sort字段进行处理

        intentionRepository.save(intention);
    }

    /**
     * 获取意向
     * @param intentionGetRequest
     * @return
     */
    @Override
    public List<IntentionVo> getIntention(IntentionGetRequest intentionGetRequest) {
        List<Intention> intentionList = intentionRepository.getIntentionByTemplateIdAndIntentionType(intentionGetRequest.getTemplateId(), intentionGetRequest.getIntentionType());

        return intentionList.stream().map(intention -> {
            IntentionVo intentionVo = new IntentionVo();
            BeanUtils.copyProperties(intention, intentionVo);
            // 处理所有条件，任意条件。从字符串变成List
            if (!intention.getAllCondition().equals("")) {
                intentionVo.setAllCondition(Arrays.asList(intention.getAllCondition().split(",")));
            } else {
                intentionVo.setAllCondition(null);
            }
            if (!intention.getAnyCondition().equals("")) {
                intentionVo.setAnyCondition(Arrays.asList(intention.getAnyCondition().split(",")));
            } else {
                intentionVo.setAnyCondition(null);
            }
            return intentionVo;
        }).sorted(Comparator.comparingInt(IntentionVo::getSort)).collect(Collectors.toList());
    }

    /**
     * 修改意向
     * @param intentionUpdateRequest
     */
    @Override
    public void updateIntention(IntentionUpdateRequest intentionUpdateRequest) {
        Intention intention = new Intention();
        BeanUtils.copyProperties(intentionUpdateRequest, intention);
        // 设置判断条件，改为字符串，“，”分隔
        String allCondition = StringUtils.join(intentionUpdateRequest.getAllCondition(), ",");
        if (allCondition == null) allCondition = "";
        intention.setAllCondition(allCondition);
        String anyCondition = StringUtils.join(intentionUpdateRequest.getAnyCondition(), ",");
        if (anyCondition == null) anyCondition = "";
        intention.setAnyCondition(anyCondition);

        intentionRepository.save(intention);
    }

    /**
     * 删除意向
     * @param intentionDeleteRequest
     */
    @Override
    public void deleteIntention(IntentionDeleteRequest intentionDeleteRequest) {
        intentionRepository.deleteById(intentionDeleteRequest.getId());
    }
}
